// 定义用户相关的仓库
import { defineStore } from "pinia";
import { UserState } from "./interface";
import { LoginData } from "@/api/hospital/type";
import { reqUserLogin } from "@/api/hospital";

const useUserStore = defineStore("user", {
  state: (): UserState => {
    return {
      visible: false,
      isLogin: false,
      userInfo: {} as any,
    };
  },
  actions: {
    changeVisible(visible: boolean) {
      this.visible = visible;
    },
    changeIsLogin(isLogin: boolean) {
      this.isLogin = isLogin;
    },
    async userLogin(loginData: LoginData) {
      let { data } = await reqUserLogin(loginData);
      this.userInfo = data;
      this.changeIsLogin(true);
      this.changeVisible(false);
    },
  },
  getters: {},
  persist: {
    // 要注意，需要持久化存储的不只是userInfo，还要有isLogin也就是标记是否已登录的布尔值
    paths: ["userInfo", "isLogin"],
  },
});

export default useUserStore;
